package cn.itcast.service.cargo.impl;

import cn.itcast.dao.cargo.*;
import cn.itcast.domain.cargo.*;
import cn.itcast.service.cargo.PackService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

@Service
public class PackServiceImpl implements PackService {

    @Autowired
    private ContractDao contractDao;

    @Autowired
    private PackDao packDao;

    @Autowired
    private ExportDao exportDao;

    @Autowired
    private ExportProductDao exportProductDao;


    @Autowired
    private ExtCproductDao extCproductDao;

    @Autowired
    private ExtEproductDao extEproductDao;


    //装箱单的分页
    @Override
    public PageInfo<Pack> findByPage(PackExample packExample, int pageNum, int pageSize) {
        //1.设置当前页与页面大小
        PageHelper.startPage(pageNum,pageSize);
        //2 .查询数据
        List<Pack> packList = packDao.selectByExample(packExample);

        //3. 构建PageInfo
        PageInfo<Pack> pageInfo = new PageInfo<>(packList);
        return pageInfo;
    }


    //根据条件查询
    @Override
    public List<Pack> findAll(PackExample packExample) {
        List<Pack> packList = packDao.selectByExample(packExample);
        return packList;
    }

    //根据主键查询装箱单
    @Override
    public Pack findById(String id) {
        return packDao.selectByPrimaryKey(id);
    }

    //生成装箱单
    @Override
    public void save(Pack pack) {
        //补全id
        pack.setPackingListId(UUID.randomUUID().toString());
        //补全创建时间与更新时间，因为这两个字段不能为空
        pack.setCreateTime(new Date());
        //设置发票合同号
        pack.setInvoiceNo(pack.getPackingListId());
        //设置合同时间
        pack.setInvoiceDate(new Date());
        //卖方
        pack.setSeller(pack.getCompanyName());

        //买方
        String[] exportIds = pack.getExportIds().split(",");
        ExportExample exportExample = new ExportExample();
        exportExample.createCriteria().andIdIn(Arrays.asList(exportIds));
        List<Export> exportList = exportDao.selectByExample(exportExample);
        for (Export export : exportList) {
            String consignee = export.getConsignee();
            export.setState(3);
            exportDao.updateByPrimaryKeySelective(export);
            pack.setBuyer(consignee);
        }

        //装箱单的状态： 刚生成到装箱单是0，草稿状态的装箱单是不允许装箱，0状态可以拿给委托装箱，1已经装箱的
        pack.setState(0l);
        //插入装箱单
        packDao.insertSelective(pack);
    }


    @Override
    public void update(Pack pack) {
    }

    //根据主键删除装箱单
    @Override
    public void delete(String id) {
        Pack pack = findById(id);
        String exportIds = pack.getExportIds();
        ExportExample exportExample = new ExportExample();
        exportExample.createCriteria().andIdIn(Arrays.asList(exportIds));
        List<Export> exportList = exportDao.selectByExample(exportExample);
        for (Export export : exportList) {
            export.setState(2);
            exportDao.updateByPrimaryKeySelective(export);
        }
        packDao.deleteByPrimaryKey(id);
    }

}
